package com.miguo.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.miguo.ui.R;

import java.util.Calendar;

/**
 * 底部导航
 */
public class FooterTabView extends LinearLayout implements View.OnClickListener {

    private Context mContext;

    private int length;

    private RelativeLayout[] mList = new RelativeLayout[5];

    public FooterTabView(Context context) {
        this(context, null);
    }

    public FooterTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 初始化控件
        mContext = context;
        this.initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        // this.setOrientation(LinearLayout.HORIZONTAL);
        this.setBackgroundResource(R.drawable.tab_bottom_bg);
        inflate(mContext, R.layout.layer_base_footer_tab, this);

        mList[0] = (RelativeLayout) findViewById(R.id.footer_tab_rl1);//首页
        mList[1] = (RelativeLayout) findViewById(R.id.footer_tab_rl2);//品牌
        mList[2] = (RelativeLayout) findViewById(R.id.footer_tab_rl3); //9.9
        mList[3] = (RelativeLayout) findViewById(R.id.footer_tab_rl4); //分类
        mList[4] = (RelativeLayout) findViewById(R.id.footer_tab_rl5);//我的
        length = mList.length;
        for (int i = 0; i < length; i++) {
            mList[i].setTag(i);
            mList[i].setOnClickListener(this);
        }

        /****  start  2016-03-11  ***/
        TextView txtDate = ((TextView)mList[0].findViewById(R.id.txtDate));
        txtDate.setText(String.valueOf(Calendar.getInstance().get(Calendar.DATE)));
        /***  end 2016-03-11  ***/
    }

    @Override
    public void onClick(View v) {
        int position = Integer.parseInt(v.getTag().toString());
        if (position > length) {
            return;
        }
        for (int i = 0; i < length; i++) {
            if (position == i) {
                mList[position].setSelected(true);
            } else {
                mList[i].setSelected(false);
            }
        }
        mTabListener.onTabSelected(position);
    }

    /**
     * 设置指示点的显示
     *
     * @param position 显示位置
     * @param visible  是否显示，如果false，则都不显示
     */

    public void setIndicateDisplay(int position, boolean visible) {
        if (position > length) {
            return;
        }
        mList[position].getChildAt(2).setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置底部导航显示状态
     */
    public void setTabsDisplay(int position) {
        if (position > length) {
            return;
        }
        for (int i = 0; i < length; i++) {
            if (position == i) {
                mList[position].setSelected(true);
            } else {
                mList[i].setSelected(false);
            }
        }
        mTabListener.onTabSelected(position);
    }


    // 回调接口，用于获取tab的选中状态
    private OnTabSelectedListener mTabListener;


    public interface OnTabSelectedListener {
        void onTabSelected(int position);
    }

    public void setOnTabSelectedListener(OnTabSelectedListener listener) {
        this.mTabListener = listener;
    }

}